import Moment from 'moment'
import { MessageBox } from 'element-ui'
export const common = {
  /**
   * @description 初始默认一个月时间
   * @param time 服务器时间
   * @param type 时间格式
   */
  initSetTime: function (time, type) {
    const { lastDayAndDay } = time
    return {
      startTime: Moment(Number(lastDayAndDay[0])).format(type),
      endTime: Moment(Number(lastDayAndDay[1])).format(type)
    }
  },
  /**
   * @description 重置
   * @param data 需要重置的数据
   * @param 默认初始值
   */
  resetData: function (data, fixedData = {}) {
    const isType = Object.prototype.toString
    for (let index in data) {
      if (isType.call(data[index]) === '[object Array]') {
        data[index] = []
      } else if (isType.call(data[index]) === '[object Object]') {
        data[index] = {}
      } else {
        data[index] = ''
      }
    }
    data = Object.assign(data, fixedData)
    return data
  },
  /**
  * @description 将一个对象的值赋给另一个对象
  */
  listAssign: function (arra, arrb) {
    Object.keys(arra).forEach(key => {
      arra[key] = (arrb[key] !== '' || arrb[key] !== null || arrb[key] !== undefined) ? arrb[key] : arra[key]
    })
  },
  /**
   * @description 提示信息框
   */
  confirmFn: async function (mTitle, bTitle) {
    let res = false
    let title = `<p style="font-weight: bold">${bTitle}</p>`
    await MessageBox.confirm(title + mTitle, '确认提示', {
      dangerouslyUseHTMLString: true,
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      res = true
    }).catch(() => {
      return false
    })
    return res
  },
  confirmBatch: async function (mTitle, bTitle, type, allCount, count) {
    let res = false
    let title = `<p style="font-weight: bold">${bTitle}</p>`
    let content = `<p>已选择 <span style="color: red">${allCount}</span> 笔${type}，可处理 <span style="color: red">${count}</span> 笔</p>`
    await MessageBox.confirm(title+ content + mTitle, '确认提示', {
      dangerouslyUseHTMLString: true,
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      res = true
    }).catch(() => {
      return false
    })
    return res
  }
}
